package com.mayisoftware.mayioa.common.impl.utils.file;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.FileHeader;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.List;


public class Zip {
    public static void main(String[] args) throws IOException {
        String source = "D:\\profile\\TMWorkTime\\a68f3b4b5d6f67772c5eded707141475.zip";
        String des = "D:\\profile\\TMWorkTime\\a68f3b4b5d6f67772c5eded707141475";

        ZipFile zipFile = new ZipFile(source);
        //防止乱码
        zipFile.setCharset(Charset.forName("GBK"));
        //查看包内所有文件名
        List<FileHeader> fileHeaders = zipFile.getFileHeaders();
        fileHeaders.stream().forEach(fileHeader ->
        {
            String aa  = "";
            try {
                aa = new String(fileHeader.getFileName().getBytes(), "GBK");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            System.out.println(aa);
        });
        //解压所有文件到目标文件夹
        zipFile.extractAll(des);
    }

    public static  String changeCharset(String str, String newCharset) {
        try{
        if (str != null) {
            //用默认字符编码解码字符串。
            byte[] bs = str.getBytes();
            //用新的字符编码生成字符串
            return new String(bs, newCharset);
        }}catch (Exception ex ){

        }
        return null;
    }

}
